package com.yida.gwj.finance.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.gwj.api.domain.GwjFinanceAccountDetail;
import com.yida.gwj.api.domain.request.RemoteAccountDetailForRateRequest;
import com.yida.gwj.api.domain.request.RemoteGwjFinanceAccountDetailRequest;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.enums.GwjCheckAccountStatus;
import com.yida.gwj.common.enums.GwjCheckAccountType;
import com.yida.gwj.finance.mapper.GwjFinanceAccountDetailMapper;
import com.yida.gwj.finance.service.IGwjFinanceAccountDetailService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;

/**
 * 银行分类帐信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-12
 */
@Service
public class GwjFinanceAccountDetailServiceImpl implements IGwjFinanceAccountDetailService {
    @Resource
    private GwjFinanceAccountDetailMapper gwjFinanceAccountDetailMapper;

    /**
     * 查询银行分类帐信息
     *
     * @param accountDetailId 银行分类帐信息主键
     * @return 银行分类帐信息
     */
    @Override
    public GwjFinanceAccountDetail selectGwjFinanceAccountDetailByAccountDetailId(Long accountDetailId) {
        return gwjFinanceAccountDetailMapper.selectGwjFinanceAccountDetailByAccountDetailId(accountDetailId);
    }

    /**
     * 查询银行分类帐信息列表
     *
     * @param gwjFinanceAccountDetail 银行分类帐信息
     * @return 银行分类帐信息
     */
    @Override
    public List<GwjFinanceAccountDetail> selectGwjFinanceAccountDetailList(GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        return gwjFinanceAccountDetailMapper.selectGwjFinanceAccountDetailList(gwjFinanceAccountDetail);
    }

    /**
     * 新增银行分类帐信息
     *
     * @param gwjFinanceAccountDetail 银行分类帐信息
     * @return 结果
     */
    @Override
    public int insertGwjFinanceAccountDetail(GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        return gwjFinanceAccountDetailMapper.insertGwjFinanceAccountDetail(gwjFinanceAccountDetail);
    }

    @Override
    public int insertBatchGwjFinanceAccountDetail(ArrayList<GwjFinanceAccountDetail> gwjFinanceAccountDetails) {
        return gwjFinanceAccountDetailMapper.insertBatchGwjFinanceAccountDetail(gwjFinanceAccountDetails);
    }

    /**
     * 修改银行分类帐信息
     *
     * @param gwjFinanceAccountDetail 银行分类帐信息
     * @return 结果
     */
    @Override
    public int updateGwjFinanceAccountDetail(GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        return gwjFinanceAccountDetailMapper.updateGwjFinanceAccountDetail(gwjFinanceAccountDetail);
    }

    /**
     * 批量删除银行分类帐信息
     *
     * @param accountDetailIds 需要删除的银行分类帐信息主键
     * @return 结果
     */
    @Override
    public int deleteGwjFinanceAccountDetailByAccountDetailIds(Long[] accountDetailIds) {
        return gwjFinanceAccountDetailMapper.deleteGwjFinanceAccountDetailByAccountDetailIds(accountDetailIds);
    }

    /**
     * 删除银行分类帐信息信息
     *
     * @param accountDetailId 银行分类帐信息主键
     * @return 结果
     */
    @Override
    public int deleteGwjFinanceAccountDetailByAccountDetailId(Long accountDetailId) {
        return gwjFinanceAccountDetailMapper.deleteGwjFinanceAccountDetailByAccountDetailId(accountDetailId);
    }

    /**
     * 根据业务ID查询银行分类帐信息列表
     *
     * @param bizId 业务ID
     * @return 银行分类帐信息集合
     */
    @Override
    public List<GwjFinanceAccountDetail> selectGwjFinanceAccountDetailListByBizId(Long bizId) {
        GwjFinanceAccountDetail gwjFinanceAccountDetail = new GwjFinanceAccountDetail();
        gwjFinanceAccountDetail.setBizId(bizId);
        return gwjFinanceAccountDetailMapper.selectGwjFinanceAccountDetailList(gwjFinanceAccountDetail);
    }

    /**
     * 根据业务ID删除银行分类帐信息信息
     *
     * @param bizId 业务ID
     * @return 结果
     */
    @Override
    public int deleteGwjFinanceAccountDetailByBizId(Long bizId) {
        return gwjFinanceAccountDetailMapper.deleteGwjFinanceAccountDetailByBizId(bizId);
    }

    /**
     * 查询银行分类帐信息列表（连表总账流水）
     *
     * @param gwjFinanceAccountDetail 银行分类帐信息
     * @return 银行分类帐信息集合
     */
    @Override
    public List<GwjFinanceAccountDetail> selectGwjFinanceAccountDetailListWithMain(GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        return gwjFinanceAccountDetailMapper.selectGwjFinanceAccountDetailListWithMain(gwjFinanceAccountDetail);
    }

    /**
     * 批量修改银行分类帐信息
     *
     * @param details 分账信息
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBatchGwjFinanceAccountDetail(ArrayList<GwjFinanceAccountDetail> details) {
        List<GwjFinanceAccountDetail> detailList = details.stream().collect(Collectors.toList());
        if (ObjectUtil.isEmpty(detailList)) {
            throw new ServiceException("分账信息为空");
        }
        detailList.forEach(item -> {
            gwjFinanceAccountDetailMapper.updateGwjFinanceAccountDetail(item);
        });
        return 1;
    }

    /**
     * 批量插入银行分类帐信息
     *
     * @param accountDetailArrayList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertBatchGwjFinanceAccountDetailInner(ArrayList<GwjFinanceAccountDetail> accountDetailArrayList) {
        List<GwjFinanceAccountDetail> detailList = accountDetailArrayList.stream().collect(Collectors.toList());
        if (ObjectUtil.isEmpty(detailList)) {
            throw new ServiceException("分账信息为空");
        }
        detailList.forEach(item -> {
            gwjFinanceAccountDetailMapper.insertGwjFinanceAccountDetail(item);
        });
        return 1;
    }

    /**
     * 查询分账列表
     *
     * @param detailRequest
     * @return
     */
    @Override
    public List<GwjFinanceAccountDetail> getGwjFinanceAccountDetailList(RemoteGwjFinanceAccountDetailRequest detailRequest) {
        GwjFinanceAccountDetail detail = new GwjFinanceAccountDetail();
        BeanUtil.copyProperties(detailRequest, detail);
        return gwjFinanceAccountDetailMapper.selectGwjFinanceAccountDetailList(detail);
    }

    @Override
    public List<GwjFinanceAccountDetail> getListForRate(RemoteAccountDetailForRateRequest request) {
        // List<String> houseCodes = request.getHouseCodes();
        // HashMap<String, Object> paramMap = new HashMap<String, Object>() {{
        // put("areaId", request.getAreaId());
        // put("startTime", request.getStartTime());
        // put("EndTime", request.getEndTime());
        // }};
        List<GwjFinanceAccountDetail> listForRate = gwjFinanceAccountDetailMapper.getListForRate(request);
        return listForRate;
    }

    @Override
    public int deleteGwjFinanceAccountDetail(GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        return gwjFinanceAccountDetailMapper.deleteGwjFinanceAccountDetail(gwjFinanceAccountDetail);
    }

    // ----------------------------------新增功能区 s---------------------------------------------------------
    /**
     * 统计分类账数据
     * 
     * @param gwjFinanceAccountDetail
     * @return
     */
    @Override
    public Map<String, BigDecimal> selectGwjFinanceAccountDetailStatistics(GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        return gwjFinanceAccountDetailMapper.selectGwjFinanceAccountDetailStatistics(gwjFinanceAccountDetail);
    }

    /* 
     * 查询银行分类帐信息列表
     */
    @Override
    public List<GwjFinanceAccountDetail> selectGwjFinanceAccountDetailListData(GwjFinanceAccountDetail gwjFinanceAccountDetail) {
        List<GwjFinanceAccountDetail> list = gwjFinanceAccountDetailMapper.selectGwjFinanceAccountDetailList(gwjFinanceAccountDetail);
        if (StringUtils.isNotEmpty(list)) {
            list.forEach(item -> {
                item.setBizFlagIdStr(GwjBusinessFlagType.getInfoByCode(item.getBizFlagId()));
                item.setCheckTypeStr(GwjCheckAccountType.getInfoByCode(item.getCheckType()));
                item.setCheckStatusStr(GwjCheckAccountStatus.getInfoByCode(item.getCheckStatus()));
            });
        }
        return list;
    }

    // ----------------------------------新增功能区 e---------------------------------------------------------
}
